package com.mybatisplus.demo.android.controller;

import com.alibaba.druid.util.StringUtils;
import com.mybatisplus.demo.android.enmu.ApiResponse;
import com.mybatisplus.demo.android.enmu.ResultCode;
import com.mybatisplus.demo.android.error.BusinessException;
import com.mybatisplus.demo.android.rep.DeviceRep;
import com.mybatisplus.demo.android.service.DeviceService;
import com.mybatisplus.demo.android.util.ApiResponseUtil;
import com.mybatisplus.demo.android.util.LoggerUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户类
 */
@Controller
@Api(value = "设备相关接口", description = "提供了设备的增删改查接口", tags = "设备管理模块", position = 3)
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @PostMapping("admin/addDevice")
    @ResponseBody
    @ApiOperation(value = "新增设备接口", notes = "新增设备接口 参数DeviceRep", httpMethod = "POST", response = ApiResponse.class)
    @ApiResponses(value = {
            @io.swagger.annotations.ApiResponse(code = 200, message = "{\"code\":200,\"message\":\"success\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 207, message = "{\"code\":209,\"message\":\"用户名不能为空\",\"data\":null}")
    })
    public ApiResponse addDevice(@Valid @RequestBody DeviceRep deviceRep, BindingResult result) throws BusinessException {
        //如果有错误
        if (result.hasErrors()) {
            return ApiResponseError(result);
        }

        ApiResponse apiResponse = deviceService.addDevice(deviceRep);

        return ApiResponseUtil.getResult(apiResponse);
    }

    @PostMapping("admin/deleteDevice")
    @ResponseBody
    @ApiOperation(value = "根据设备sn号删除设备接口", notes = "根据设备sn号删除", httpMethod = "POST", response = ApiResponse.class)
    @ApiImplicitParam(name = "deveice_sn", value = "设备编码", required = true, dataType = "String", paramType = "query")
    @ApiResponses(value = {
            @io.swagger.annotations.ApiResponse(code = 200, message = "{\"code\":200,\"message\":\"删除成功\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 212, message = "{\"code\":212,\"message\":\"参数错误\",\"data\":null}")
    })
    public ApiResponse deleteDevice(String deveice_sn) throws BusinessException {
        if (StringUtils.isEmpty(deveice_sn)) {
            LoggerUtil.error("deveice_sn is null", DeviceController.class);
            throw new BusinessException(ResultCode.PARAM_ERROR);
        }
        LoggerUtil.info("deveice_sn : " + deveice_sn, DeviceController.class);
        ApiResponse apiResponse = deviceService.deleteDevice(deveice_sn);

        return ApiResponseUtil.getResult(apiResponse);
    }

    @PostMapping("admin/updateDevice")
    @ResponseBody
    @ApiOperation(value = "变更设备信息接口", notes = "变更设备信息接口,根据设备sn变更设备信息", httpMethod = "POST", response = ApiResponse.class)
    @ApiResponses(value = {
            @io.swagger.annotations.ApiResponse(code = 200, message = "{\"code\":200,\"message\":\"修改成功\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 209, message = "{\"code\":209,\"message\":\"参数错误\",\"data\":null}")
    })
    public ApiResponse updateDevice(@Valid @RequestBody DeviceRep deviceRep, BindingResult result) throws BusinessException {
        //如果有错误
        if (result.hasErrors()) {
            return ApiResponseError(result);
        }

        ApiResponse apiResponse = deviceService.updateDevice(deviceRep);

        return ApiResponseUtil.getResult(apiResponse);
    }

    @PostMapping("admin/queryDevice")
    @ResponseBody
    @ApiOperation(value = "根据设备sn号查询设备信息接口", notes = "根据设备sn号查询", httpMethod = "POST", response = ApiResponse.class)
    @ApiImplicitParam(name = "deveice_sn", value = "设备编码", required = true, dataType = "String", paramType = "query")
    @ApiResponses(value = {
            @io.swagger.annotations.ApiResponse(code = 200, message = "{\"code\":200,\"message\":\"success\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 212, message = "{\"code\":212,\"message\":\"参数错误\",\"data\":null}")
    })
    public ApiResponse selectOneDevice(String deveice_sn) throws BusinessException {
        if (StringUtils.isEmpty(deveice_sn)) {
            LoggerUtil.error("deveice_sn is null", DeviceController.class);
            throw new BusinessException(ResultCode.DEVICE_NOT_ISEXIST);
        }
        ApiResponse apiResponse = deviceService.selectOneDevice(deveice_sn);

        return ApiResponseUtil.getResult(apiResponse);
    }

    /**
     * 根据设备类型查询设备列表
     *
     * @param deveice_type
     * @return
     * @throws BusinessException
     */
    @PostMapping("admin/queryTypeDevicesList")
    @ResponseBody
    @ApiOperation(value = "根据设备类型查询查询设备列表接口", notes = "根据设备类型查询", httpMethod = "POST", response = ApiResponse.class)
    @ApiImplicitParam(name = "deveice_type", value = "设备类型", required = true, dataType = "String", paramType = "query")
    @ApiResponses(value = {
            @io.swagger.annotations.ApiResponse(code = 200, message = "{\"code\":200,\"message\":\"success\",\"data\":{[{\"userId\":\"15103747270\",\"deveice_sn\":\"10001\",\"deveice_name\":\"空气检测仪\",\"deveice_model\":\"sn10001\",\"deveice_type\":\"1\",\"manufacturer\":\"苏州二厂\",\"production_date\":\"2020-02-27\",\"deveice_state\":\"正常\"}]}"),
            @io.swagger.annotations.ApiResponse(code = 212, message = "{\"code\":212,\"message\":\"参数错误\",\"data\":null}")
    })
    public ApiResponse selectTypeListDevices(String deveice_type) throws BusinessException {
        if (StringUtils.isEmpty(deveice_type)) {
            LoggerUtil.error("error : " + ResultCode.DEVICE_NOT_ISEXIST.getMsg(), DeviceController.class);
            throw new BusinessException(ResultCode.DEVICE_NOT_ISEXIST);
        }
        ApiResponse apiResponse = deviceService.selectTypeListDevices(deveice_type);

        return ApiResponseUtil.getResult(apiResponse);
    }

    /**
     * 根据用户id查询设备列表
     *
     * @param userId
     * @return
     * @throws BusinessException
     */
    @PostMapping("admin/queryUserIdDevicesList")
    @ResponseBody
    @ApiOperation(value = "根据用户id查询设备列表接口", notes = "根据用户id查询", httpMethod = "POST", response = ApiResponse.class)
    @ApiImplicitParam(name = "userId", value = "用户手机号", required = true, dataType = "String", paramType = "header")
    @ApiResponses(value = {
            @io.swagger.annotations.ApiResponse(code = 200, message = "{\"code\":200,\"message\":\"success\",\"data\":[{\"userId\":\"15103747270\",\"deveice_sn\":\"10001\",\"deveice_name\":\"空气检测仪\",\"deveice_model\":\"sn10001\",\"deveice_type\":\"1\",\"manufacturer\":\"苏州二厂\",\"production_date\":\"2020-02-27\",\"deveice_state\":\"正常\"}]}"),
            @io.swagger.annotations.ApiResponse(code = 212, message = "{\"code\":212,\"message\":\"参数错误\",\"data\":null}")
    })
    public ApiResponse selectUserIdListDevices(@RequestHeader String userId) throws BusinessException {
        if (StringUtils.isEmpty(userId)) {
            LoggerUtil.error("error : " + ResultCode.DEVICE_NOT_ISEXIST.getMsg(), DeviceController.class);
            throw new BusinessException(ResultCode.DEVICE_NOT_ISEXIST);
        }
        ApiResponse apiResponse = deviceService.selectUserIdListDevices(userId);

        return ApiResponseUtil.getResult(apiResponse);
    }


    /**
     * 验证如果参数有错误 则返回错误信息
     *
     * @param result
     * @return
     */
    private ApiResponse ApiResponseError(BindingResult result) {

        ApiResponse apiResponse1 = new ApiResponse();
        List<ObjectError> ls = result.getAllErrors();
        for (int i = 0; i < ls.size(); i++) {
            LoggerUtil.error("error : " + ls.get(i), DeviceController.class);
            apiResponse1.setResultCode(ResultCode.PARAM_ERROR);
            apiResponse1.setMessage(ls.get(i).getDefaultMessage());
            break;
        }
        return ApiResponseUtil.getResult(apiResponse1);
    }
}

























